package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.repository.SalesPlanDetailRepository;
import com.alibaba.citrus.ots.common.annotation.RepositoryInvoker;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcorder.model.dto.MonthSalesPlanProgressReportSDO;
import com.epoch.app.bcorder.model.dto.OrgMonthPlanQuantitySDO;
import com.epoch.app.bcorder.sales_plan.monthlyplanofccommerce.dto.QueryMonthPlanConfirmCountRequest;
import com.epoch.app.bcorder.sales_plan.monthlyplanofccommerce.model.MonthlyPlanOfCcommerce;
import com.epoch.app.bcorder.sales_plan.monthlyplanofccommerce.service.MonthlyPlanOfCcommerceService;
import com.epoch.app.bcorder.sales_plan.planscitemextend.dto.PageStatisticalCompanyReturnCountByReadonlyRequest;
import com.epoch.app.bcorder.sales_plan.planscitemextend.dto.PageStatisticalCompanyReturnCountRequest;
import com.epoch.app.bcorder.sales_plan.planscitemextend.dto.QueryCompanyAdjustCountByReadonlyRequest;
import com.epoch.app.bcorder.sales_plan.planscitemextend.dto.QueryCompanyAdjustCountRequest;
import com.epoch.app.bcorder.sales_plan.planscitemextend.service.PlanScItemExtendService;
import com.epoch.app.bcorder.sales_plan.salesplan.dto.LoadSalesPlanListRequest;
import com.epoch.app.bcorder.sales_plan.salesplan.model.SalesPlan;
import com.epoch.app.bcorder.sales_plan.salesplan.service.SalesPlanService;
import com.epoch.app.bcorder.sales_plan.salesplandetail.dto.PageStatisticalMonthPlanProgressDataByReadonlyRequest;
import com.epoch.app.bcorder.sales_plan.salesplandetail.dto.PageStatisticalMonthPlanProgressDataRequest;
import com.epoch.app.bcorder.sales_plan.salesplandetail.dto.PageStatisticalOrganizationTaskCountByReadonlyRequest;
import com.epoch.app.bcorder.sales_plan.salesplandetail.dto.PageStatisticalOrganizationTaskCountRequest;
import com.epoch.app.bcorder.sales_plan.salesplandetail.service.SalesPlanDetailService;
import com.epoch.app.bcorder.sales_plan.salesplanscitem.dto.BatchQueryScItemPriceByOrganizationByReadonlyRequest;
import com.epoch.app.bcorder.sales_plan.salesplanscitem.dto.BatchQueryScItemPriceByOrganizationRequest;
import com.epoch.app.bcorder.sales_plan.salesplanscitem.model.SalesPlanScItem;
import com.epoch.app.bcorder.sales_plan.salesplanscitem.service.SalesPlanScItemService;
import com.epoch.app.bcorder.salesplannyk.dto.StatisticalMonthPlanConfirmCountRequest;
import com.epoch.app.bcorder.salesplannyk.service.SalesPlanNykService;
import com.epoch.app.otsapplicationreportsdk.contants.OtsApplicationReportSdkConstants;
import com.epoch.app.otsapplicationreportsdk.model.dto.CommPageQueryRequest;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/11/23 21:56
 */
@Component
public class SalesPlanDetailRepositoryImpl implements SalesPlanDetailRepository {

    @Resource
    SalesPlanService salesPlanService;

    @Resource
    SalesPlanDetailService salesPlanDetailService;

    @Resource
    PlanScItemExtendService planScItemExtendService;

    @Resource
    SalesPlanNykService salesPlanNykService;

    @Resource
    MonthlyPlanOfCcommerceService monthlyPlanOfCcommerceService;

    @Resource
    SalesPlanScItemService salesPlanScItemService;

    @Override
    public PageResult<List<MonthSalesPlanProgressReportSDO>> pageStatisticalMonthPlanProgressData(CommPageQueryRequest request) {
        Result<List<MonthSalesPlanProgressReportSDO>> result = null;
        if("1".equals(OtsApplicationReportSdkConstants.QUERY_READONLY_DATABASE())){
            PageStatisticalMonthPlanProgressDataByReadonlyRequest queryRequest = PageStatisticalMonthPlanProgressDataByReadonlyRequest.builder()
                    .planMonth(request.getPlanMonth())
                    .orgBusinessUnitCode(request.getOrgBusinessUnitCode())
                    .customerCode(request.getCustomerCode())
                    .customerAddressCode(request.getCustomerAddressCode())
                    .scItemId(request.getScItemId())
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .build();
            result = salesPlanDetailService.pageStatisticalMonthPlanProgressDataByReadonly(queryRequest);
        }else{
            PageStatisticalMonthPlanProgressDataRequest queryRequest = PageStatisticalMonthPlanProgressDataRequest.builder()
                    .planMonth(request.getPlanMonth())
                    .orgBusinessUnitCode(request.getOrgBusinessUnitCode())
                    .customerCode(request.getCustomerCode())
                    .customerAddressCode(request.getCustomerAddressCode())
                    .scItemId(request.getScItemId())
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .build();
            result = salesPlanDetailService.pageStatisticalMonthPlanProgressData(queryRequest);
        }
        return PageResult.of(result.getResult(),result.getTotal());
    }

    @Override
    public PageResult<List<OrgMonthPlanQuantitySDO>> pageStatisticalTaskCountByOrganiztion(CommPageQueryRequest request) {
        Result<List<OrgMonthPlanQuantitySDO>> result = null;
        if("1".equals(OtsApplicationReportSdkConstants.QUERY_READONLY_DATABASE())){
            PageStatisticalOrganizationTaskCountByReadonlyRequest queryRequest = PageStatisticalOrganizationTaskCountByReadonlyRequest.builder()
                    .planMonth(request.getPlanMonth())
                    .organizationIds(request.getOrganizationIds())
                    .bizType(String.valueOf(request.getBizType()))
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .build();
            result = salesPlanDetailService.pageStatisticalOrganizationTaskCountByReadonly(queryRequest);
        }else{
            PageStatisticalOrganizationTaskCountRequest queryRequest = PageStatisticalOrganizationTaskCountRequest.builder()
                    .planMonth(request.getPlanMonth())
                    .organizationIds(request.getOrganizationIds())
                    .bizType(String.valueOf(request.getBizType()))
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .build();
            result = salesPlanDetailService.pageStatisticalOrganizationTaskCount(queryRequest);
        }
        return PageResult.of(result.getResult(),result.getTotal());
    }

    @Override
    public PageResult<List<OrgMonthPlanQuantitySDO>> pageStatisticalNykMonthPlanConfirmCount(CommPageQueryRequest request) {
        StatisticalMonthPlanConfirmCountRequest queryRequest = StatisticalMonthPlanConfirmCountRequest.builder()
                .planMonth(request.getPlanMonth())
                .start(request.getStart())
                .limit(request.getLimit())
                .build();
        Result<List<OrgMonthPlanQuantitySDO>> result = salesPlanNykService.statisticalMonthPlanConfirmCount(queryRequest);
        return PageResult.of(result.getResult(),result.getTotal());
    }

    @Override
    public PageResult<List<OrgMonthPlanQuantitySDO>> pageStatisticalCommerceMonthPlanConfirmCount(CommPageQueryRequest request) {
        com.epoch.app.bcorder.sales_plan.monthlyplanofccommerce.dto.StatisticalMonthPlanConfirmCountRequest queryRequest = com.epoch.app.bcorder.sales_plan.monthlyplanofccommerce.dto.StatisticalMonthPlanConfirmCountRequest.builder()
                .planMonth(request.getPlanMonth())
                .start(request.getStart())
                .limit(request.getLimit())
                .build();
        Result<List<OrgMonthPlanQuantitySDO>> result = monthlyPlanOfCcommerceService.statisticalMonthPlanConfirmCount(queryRequest);
        return PageResult.of(result.getResult(),result.getTotal());
    }

    @Override
    public PageResult<List<MonthlyPlanOfCcommerce>> pageQueryCommerceMonthPlanConfirmCount(CommPageQueryRequest request) {
        QueryMonthPlanConfirmCountRequest queryRequest = QueryMonthPlanConfirmCountRequest.builder()
                .planMonth(request.getPlanMonth())
                .start(request.getStart())
                .limit(request.getLimit())
                .build();
        Result<List<MonthlyPlanOfCcommerce>> result = monthlyPlanOfCcommerceService.queryMonthPlanConfirmCount(queryRequest);
        return PageResult.of(result.getResult(),result.getTotal());
    }

    @Override
    public PageResult<List<SalesPlanScItem>> batchQueryScItemPriceByOrganization(CommPageQueryRequest request) {
        Result<List<SalesPlanScItem>> result = null;
        BatchQueryScItemPriceByOrganizationRequest queryRequest = BatchQueryScItemPriceByOrganizationRequest.builder()
                .scItemIds(request.getScItemIds())
                .bizType(request.getBizType())
                .build();
        if(Objects.equals(request.getBizType(),103)){
            queryRequest.setOrgBusinessUnitCodes(request.getOrganizationIds());
        }else if(Objects.equals(request.getBizType(),104)){
            queryRequest.setOrgSalesOrganizationCodes(request.getOrganizationIds());
        }else if(Objects.equals(request.getBizType(),105)){
            queryRequest.setOrgSalesChannelCodes(request.getOrganizationIds());
        }
        queryRequest.setStart(request.getStart());
        queryRequest.setLimit(request.getLimit());
        if("1".equals(OtsApplicationReportSdkConstants.QUERY_READONLY_DATABASE())){
            BatchQueryScItemPriceByOrganizationByReadonlyRequest queryReadonlyRequest = new BatchQueryScItemPriceByOrganizationByReadonlyRequest();
            BeanUtils.copyProperties(queryRequest,queryReadonlyRequest);
            result = salesPlanScItemService.batchQueryScItemPriceByOrganizationByReadonly(queryReadonlyRequest);
        }else{
            result = salesPlanScItemService.batchQueryScItemPriceByOrganization(queryRequest);
        }
        return PageResult.of(result.getResult(),result.getTotal());
    }

    @Override
    @RepositoryInvoker
    public PageResult<List<OrgMonthPlanQuantitySDO>> pageStatisticalReturnCountByOrganiztion(CommPageQueryRequest request) {
        Result<List<OrgMonthPlanQuantitySDO>> result = null;
        if("1".equals(OtsApplicationReportSdkConstants.QUERY_READONLY_DATABASE())){
            PageStatisticalCompanyReturnCountByReadonlyRequest queryRequest = PageStatisticalCompanyReturnCountByReadonlyRequest.builder()
                    .orgBusinessUnitIds(request.getOrganizationIds())
                    .planMonth(request.getPlanMonth())
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .build();
            result = planScItemExtendService.pageStatisticalCompanyReturnCountByReadonly(queryRequest);
        }else{
            PageStatisticalCompanyReturnCountRequest queryRequest = PageStatisticalCompanyReturnCountRequest.builder()
                    .orgBusinessUnitIds(request.getOrganizationIds())
                    .planMonth(request.getPlanMonth())
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .build();
            result = planScItemExtendService.pageStatisticalCompanyReturnCount(queryRequest);
        }
        if(Objects.isNull(result)){
            return PageResult.of(new ArrayList<>(),0);
        }
        return PageResult.of(result.getResult(),result.getTotal());
    }

//    @Override
//    @RepositoryInvoker
//    public List<OrgMonthPlanQuantitySDO> statisticalAdjustCountByOrganiztion(CommPageQueryRequest request) {
//        StatisticalCompanyAdjustCountRequest queryRequest = StatisticalCompanyAdjustCountRequest.builder()
//                .planMonth(request.getPlanMonth())
//                .build();
//        Result<List<OrgMonthPlanQuantitySDO>> result = planScItemExtendService.statisticalCompanyAdjustCount(queryRequest);
//        return result.getResult();
//    }

    @Override
    public List<OrgMonthPlanQuantitySDO> queryAdjustCountByOrganiztion(CommPageQueryRequest request) {
        Result<List<OrgMonthPlanQuantitySDO>> result = null;
        if("1".equals(OtsApplicationReportSdkConstants.QUERY_READONLY_DATABASE())){
            QueryCompanyAdjustCountByReadonlyRequest queryRequest = QueryCompanyAdjustCountByReadonlyRequest.builder()
                    .orgBusinessUnitIds(request.getOrganizationIds())
                    .planMonth(request.getPlanMonth())
                    .build();
            result = planScItemExtendService.queryCompanyAdjustCountByReadonly(queryRequest);
        }else{
            QueryCompanyAdjustCountRequest queryRequest = QueryCompanyAdjustCountRequest.builder()
                    .orgBusinessUnitIds(request.getOrganizationIds())
                    .planMonth(request.getPlanMonth())
                    .build();
            result = planScItemExtendService.queryCompanyAdjustCount(queryRequest);
        }
        return result.getResult();
    }

    @Override
    public List<SalesPlan> getAdjustPlanList(CommPageQueryRequest request){
        LoadSalesPlanListRequest queryRequest = LoadSalesPlanListRequest.builder()
                .planTime(request.getPlanMonth())
                .model(request.getModel())
                .orgBusinessUnitCode(request.getOrgBusinessUnitCode())
                .start(request.getStart())
                .limit(request.getLimit())
                .build();
        Result<List<SalesPlan>> result = salesPlanService.loadSalesPlanList(queryRequest);
        List<SalesPlan> list = new ArrayList<>();
        if(result != null && CollectionUtils.isNotEmpty(result.getResult())){
            list = result.getResult();
        }
        return list;
    }

}
